import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'
import { Loading } from 'element-ui'

// axios.defaults.baseURL = "http://120.79.70.140:8081"
    axios.defaults.baseURL = "http://localhost:8081"

let loading;

function startLoading() {
    // console.log('dddddd')

    loading = Loading.service({
        lock: true,
        text: '拼命加载中...',
        background: 'rgba(255,255,255,0.5)',
    })

}




// 前置拦截

const _this = this
axios.interceptors.request.use(config => {
    startLoading()
    return config
})

axios.interceptors.response.use(response => {
        loading.close();
        let res = response.data;

        console.log("=================")
        console.log(res)
        console.log("=================")
        if (res.code === 200) {
            return response
        } else {

            Element.Message.error('axios错误400 错了哦，这是一条错误消息', { duration: 3 * 1000 })

            return Promise.reject(response.data.msg)
        }
    },
    error => {
        loading.close();
        console.log('axiosError');

        console.log(error)
        if (error.response.data) {
            console.dir(error)
            error.message = error.response.data.msg
        }
        console.log(error.message)
        if (error.response.status === 401) {
            store.commit("REMOVE_INFO")
            router.push("/login")
        }
        // Element.Message.error(信息，时间）
        Element.Message.error(error.message, { duration: 3 * 1000 })
        Element.Message.error(error.message, { duration: 3 * 1000 })
        return Promise.reject(error)
        // Promise.reject(error)拒绝想后面请求并且弹出 error信息
    }
)